<?php
/**
 * Created by novebeta.
 * Date: 10/19/12
 * Time: 7:14 PM
 */
class CmsU
{
    static public function menuAdmin()
    {
        return array(
            array(
                'label' => 'Berita',
                'url' => array('#'),
                'items' => array(
                    array('label' => 'Tambah Berita', 'url' => array('backend/cniberita/create')),
                    array('label' => 'Daftar Berita', 'url' => array('backend/cniberita/admin')),
                    array('label' => 'Tambah Kategori Berita', 'url' => array('backend/cniberita/createkategori')),
                    array('label' => 'Daftar Kategori Berita', 'url' => array('backend/cniberita/adminkategori')),)),
            array(
                'label' => 'Produk',
                'url' => array('#'),
                'items' => array(
                    array('label' => 'Tambah Produk', 'url' => array('backend/cniberita/create')),
                    array('label' => 'Daftar Produk', 'url' => array('backend/cniberita/index')),)),
            array('label' => 'Buku Tamu', 'url' => array('#')),
            array('label' => 'Header Slide Show', 'url' => array('#')),
            array(
                'label' => 'Aplikasi',
                'url' => array('#'),
                'items' => array(
                    array('label' => 'Tambah Aplikasi', 'url' => array('backend/Aplikasi/create')),
                    array('label' => 'Daftar Aplikasi', 'url' => array('backend/Aplikasi/admin')),)),
            array(
                'label' => 'Halaman',
                'url' => array('#'),
                'items' => array(
                    array('label' => 'Tambah Halaman', 'url' => array('backend/cnihalaman/create')),
                    array('label' => 'Daftar Halaman', 'url' => array('backend/cnihalaman/admin/1')),)),);
    }

    static public function frontMenu($topPage, $recursive = false)
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition("top_halaman = $topPage");
        $criteria->addCondition("halaman_id != 1");
        $criteria->order = 'urut_halaman';
        $pages = Page::model()->findAll($criteria);
        $ui = array();
        // $kategori_halaman=$page['page_name'];
        foreach ($pages as $page) {
            $nama = lang() == "id" ? $page['nama_halaman'] : $page['page_name'];
            if ($page['kategori_halaman'] == 0) $url = url('page/view', array(
                'id' =>
                lang() == "id" ? $page['slug_id'] : $page['slug_en'])); //array('/page/view/' . $page['halaman_id']);
            else
                $url = array($page['file_include']);
            $items = array();
            if ($recursive) $items = CmsU::frontMenu($page['halaman_id'], $recursive);
            if (count($items) > 0) $ui[] = array('label' => $nama, 'url' => $url, 'items' => $items); else
                $ui[] = array('label' => $nama, 'url' => $url);
        }
        return $ui;
    }

    static public function tataHalaman($argIDKey, $argJmlLevel)
    {
        $global_jumlah_level = 5;
        $criteria = new CDbCriteria();
        $criteria->addCondition("halaman_id!=1");
        $criteria->addCondition("halaman_id=$argIDKey");
        $halaman = CniHalaman::model()->findAll($criteria);
        foreach ($halaman as $page) {
            $top_halaman = $page['top_halaman'];
            $nama_halaman = $page['nama_halaman'];
            $kodeAkun = $page['urut_halaman'];
            $idNya = $page['halaman_id'];
        }
        $noUpNya = $kodeAkun - 1;
        $noDownNya = $kodeAkun + 1;
        $idUpNya = CmsU::cekHalamanAtasBawah($top_halaman, $noUpNya);
        $idDownNya = CmsU::cekHalamanAtasBawah($top_halaman, $noDownNya);
        $idRightNya = CmsU::cekHalamanAtasBawah($top_halaman, $noUpNya);
        $namaUpNya = CmsU::dataHalaman("nama", $idUpNya);
        $namaLeftNya = CmsU::dataHalaman("nama", CmsU::dataHalaman("top", $top_halaman));
        if ($namaLeftNya == "Root") $namaLeftNya = "halaman Utama"; else $namaLeftNya = "sub halaman " . $namaLeftNya;
        ///Pengunaan menu
        $lock1 = true;
        $lock2 = false;
//        if (client_key == md5("stratos")) {
//            $lock1 = false;
//            $lock2 = true;
//        } else if (client_key == md5("dylandy")) {
//            $lock1 = true;
//            $lock2 = true;
//        } else if (client_key == md5("dynames")) {
//            $lock1 = true;
//            $lock2 = false;
//        }
        if ($lock1 == true) {
            $strLeft = '<img src="' . bu() . '/images/dis_back.gif" border="0">';
            $strRight = '<img src="' . bu() . '/images/dis_forward.gif" border="0">';
            $strUp = '<img src="' . bu() . '/images/dis_up.gif" border="0">';
            $strDown = '<img src="' . bu() . '/images/dis_down.gif" border="0">';
            $strInUp = '<img src="' . bu() . '/images/bs_inup.gif" border="0">';
            $strInDown = '<img src="' . bu() . '/images/bs_indown.gif" border="0">';
            $strLeft2 = '<a href="halamanList.php?act=left&idLeftNya=' . $top_halaman . '&noAsliNya=' . $kodeAkun .
                '&idAsliNya=' . $argIDKey . '"><img src="' . bu() .
                '/images/bs_back.gif" border="0" alt="Merubah halaman ' . $nama_halaman . ' menjadi ' . $namaLeftNya .
                '" title="Merubah halaman ' . $nama_halaman . ' menjadi ' . $namaLeftNya . '"></a>';
            $strRight2 =
                '<a href="halamanList.php?act=right&idRightNya=' . $idRightNya . '&topAsliNya=' . $top_halaman .
                    '&noAsliNya=' . $kodeAkun . '&idAsliNya=' . $argIDKey . '"><img src="' . bu() .
                    '/images/bs_forward.gif" border="0" alt="Merubah halaman ' . $nama_halaman .
                    ' menjadi sub halaman ' . $namaUpNya . '" title="Merubah halaman ' . $nama_halaman .
                    ' menjadi sub halaman ' . $namaUpNya . '"></a>';
            $strUp2 = '<a href="halamanList.php?act=up&noUpNya=' . $noUpNya . '&idUpNya=' . $idUpNya . '&noAsliNya=' .
                $kodeAkun . '&idAsliNya=' . $argIDKey . '"><img src="' . bu() .
                '/images/bs_up.gif" border="0" alt="Menaikkan halaman ' . $nama_halaman . ' menjadi urutan ke ' .
                $noUpNya . '" title="Menaikkan halaman ' . $nama_halaman . ' menjadi urutan ke ' . $noUpNya . '"></a>';
            $strDown2 = '<a href="halamanList.php?act=down&noDownNya=' . $noDownNya . '&idDownNya=' . $idDownNya .
                '&noAsliNya=' . $kodeAkun . '&idAsliNya=' . $argIDKey . '"><img src="' . bu() .
                '/images/bs_down.gif" border="0" alt="Menurunkan halaman ' . $nama_halaman . ' menjadi urutan ke ' .
                $noDownNya . '" title="Menurunkan halaman ' . $nama_halaman . ' menjadi urutan ke ' . $noDownNya .
                '"></a>';
            $strInUp2 = '<a href="halamanAdd.php?act=insertUP&topTU=' . $top_halaman . '&noAsliNya=' . $kodeAkun .
                '"><img src="' . bu() . '/images/bs_inup.gif" border="0" alt="Tambah halaman diatas ' . $nama_halaman .
                '" title="Tambah halaman diatas ' . $nama_halaman . '"></a>';
            $strInDown2 =
                '<a href="halamanAdd.php?act=insertDown&topTU=' . $top_halaman . '&noAsliNya=' . ($kodeAkun + 1) .
                    '"><img src="' . bu() . '/images/bs_indown.gif" border="0" alt="Tambah halaman dibawah ' .
                    $nama_halaman . '" title="Tambah halaman dibawah ' . $nama_halaman . '"></a>';
            if ($top_halaman == "1") {
                $strHapus = '&nbsp;';
            } else {
                $strHapus = '<a href="halamanDel.php?act=del&topTU=' . $top_halaman . '&noAsliNya=' . ($kodeAkun + 1) .
                    '&idNya=' . $idNya . '"><img src="' . bu() . '/images/delete.png" border="0" alt="Hapus Halaman ' .
                    $nama_halaman . '" title="Hapus Halaman ' . $nama_halaman . '"></a>';
            }
        }
        if ($top_halaman < 2) $strLeft2 = $strLeft;
        if ($idUpNya == 0 or $noUpNya == 0) $strUp2 = $strUp;
        if ($idDownNya == 0 or $noDownNya == 0) $strDown2 = $strDown;
        if ($kodeAkun == 1) $strRight2 = $strRight;
        $jumlahTotalLevel = 0;
        $jumlahTotalLevel = CmsU::func_jumlahTotalLevel($idNya, 1, 0, 0);
        if (($argJmlLevel + $jumlahTotalLevel) >= $global_jumlah_level) $strRight2 = $strRight;
        if ($lock2 == true) {
            /* if($top_halaman=="1") {
                $strUp2 = '&nbsp;';
                $strDown2 = '&nbsp;';
            } */
            $strLeft2 = '&nbsp;';
            $strRight2 = '&nbsp;';
            $strInUp2 = '&nbsp;';
            $strInDown2 = '&nbsp;';
            $strHapus = '&nbsp;';
        }
        echo'<td align="center" valign="top">' . $strLeft2 . '</td><td align="center" valign="top">' . $strRight2 .
            '</td><td align="center" valign="top">' . $strUp2 . '</td><td align="center" valign="top">' . $strDown2 .
            '</td><td align="center" valign="top">' . $strInUp2 . '</td><td align="center" valign="top">' .
            $strInDown2 . '</td><td align="center" valign="top">' . $strHapus . '</td>';
    }

    function dataHalaman($strArg, $intMember)
    {
        $page = CniHalaman::model()->findByPk($intMember);
//        $sqlnama_kategori = "select * from " . tabel_halaman . " WHERE halaman_id='" . $intMember . "'";
//        $gonama_kategori = mysql_query($sqlnama_kategori);
//        while ($rs_nama_kategori = mysql_fetch_array($gonama_kategori)) {
        $top_kategori = $page->top_halaman;
        $nama_kategori = $page->nama_halaman;
        $kodeAkun = $page->urut_halaman;
//        }
        if ($kodeAkun == "0") $kodeAkun = "";
        if ($strArg == "top") return $top_kategori;
        if ($strArg == "nama") return $nama_kategori;
        if ($strArg == "urut") return $kodeAkun;
    }

    function cekHalamanAtasBawah($argTop, $argID)
    {
        //$sqlAB = "SELECT * FROM " . tabel_halaman . " WHERE top_halaman='" . $argTop . "' and urut_halaman='" . $argID . "' ORDER BY urut_halaman";
        $criteria = new CDbCriteria();
        $criteria->addCondition("top_halaman='$argTop'");
        $criteria->addCondition("urut_halaman='$argID'");
        $criteria->order = 'urut_halaman';
//        $res_AB = mysql_query($sqlAB) or die(mysql_error());
//        $cekHalamanAtasBawah = 0;
//        while ($rs_AB = mysql_fetch_array($res_AB)) {
//            $cekHalamanAtasBawah = $rs_AB[halaman_id];
//        }
        $pages = CniHalaman::model()->findAll($criteria);
        $cekHalamanAtasBawah = 0;
        foreach ($pages as $page) {
            $cekHalamanAtasBawah = $page['halaman_id'];
        }
        return $cekHalamanAtasBawah;
    }

    function func_jumlahTotalLevel($idParentCategory2, $idPreviousParentCategory, $tabs, $maktabs)
    {
//        $mySql2 = "SELECT * FROM " . tabel_halaman . " WHERE top_halaman=" . $idParentCategory2 . " ORDER BY urut_halaman";
        $criteria = new CDbCriteria();
        $criteria->addCondition("top_halaman=$idParentCategory2");
        $criteria->order = 'urut_halaman';
//        $res_mySQL2 = mysql_query($mySql2);
        $pages = CniHalaman::model()->findAll($criteria);
        foreach ($pages as $page) {
            $pIdCategory2 = $page['halaman_id'];
            if ($idPreviousParentCategory != $idParentCategory2) {
                if ($idPreviousParentCategory > $idParentCategory2) {
                    $tabs = $tabs - 1;
                } else {
                    $tabs = $tabs + 1;
                }
                $idPreviousParentCategory = $idParentCategory2;
            }
            $maktabs = $maktabs + 1;
            //echo $maktabs."--".$tabs."-->".$pIdCategory2."<br>";
            CmsU::func_jumlahTotalLevel($pIdCategory2, $idPreviousParentCategory, $tabs, $maktabs);
        }
        return $maktabs;
    }

    static public function func_ambilHalaman($idParentCategory2, $idPreviousParentCategory, $tabs, $jmlKolom)
    {
        //$globalTypeAkun =
        $global_jumlah_level = 5;
//        $mySql2 = "SELECT * FROM " . tabel_halaman . " WHERE top_halaman=" . $idParentCategory2 . " ORDER BY urut_halaman";
        $criteria = new CDbCriteria();
        $criteria->addCondition("top_halaman=$idParentCategory2");
        $criteria->order = 'urut_halaman';
        $pages = CniHalaman::model()->findAll($criteria);
        foreach ($pages as $page) {
            $pIdCategory2 = $page['halaman_id'];
            $pCategoryDesc2 = $page['nama_halaman'];
            $purut_halaman2 = $page['urut_halaman'];
            $pStatus2 = $page['status_halaman'];
            $warnaTulisan = '';
            $warnaTulisan2 = '';
            if ($pStatus2 != 1) {
                $warnaTulisan = '<font color="red">';
                $warnaTulisan2 = '</font>';
            }
            if ($idPreviousParentCategory != $idParentCategory2) {
                if ($idPreviousParentCategory > $idParentCategory2) {
                    $tabs = $tabs - 1;
                } else {
                    $tabs = $tabs + 1;
                }
                $idPreviousParentCategory = $idParentCategory2;
            }
            echo '<tr bgcolor="#FBFDEE" onMouseOver="this.style.background=\'#EDF1A5\'" onMouseOut="this.style.background=\'#FBFDEE\'">';
            for ($f = 1; $f <= $tabs; $f++) {
                echo '<td align="left" valign="top" bgcolor="#FBFDEE" width="1%">&nbsp;</td>';
            }
            echo'<td align="left" valign="top" colspan="' . (($jmlKolom - $f) + 1) .
                '"><a href=halamanEdit.php?act=edit&idNya=' . $pIdCategory2 . '>' . $warnaTulisan . ' ' .
                $pCategoryDesc2 . $warnaTulisan2 . '</a> ';
            echo '</td>';
            echo CmsU::tataHalaman($pIdCategory2, $f) . '</tr>';
            if ($f < $global_jumlah_level) CmsU::func_ambilHalaman($pIdCategory2, $idPreviousParentCategory, $tabs,
                $jmlKolom);
        }
    }

    static public function RemoveAll($dir, $patern = "*")
    {
        array_map('unlink', glob($dir . $patern));
    }

    static public function break_words($words, $length, $style = "")
    {
        $words = str_replace("<br>", " ", $words);
        $words = str_replace("<p>", "", $words);
        $words = str_replace("</p>", "", $words);
        if (strlen($words) > $length) {
            $spasi = strpos($words, " ", $length);
            $words = substr($words, 0, $spasi);
            if ($spasi == "") $words = $words;
        }
        if (strlen($words) < $length) $words = $words;
        // $words='<p '.$style.'>'.$words.'... </p>';
        return $words;
    }

    static public function getImage($words, $width, $height, $style = "")
    {
        $image = '';
        $sRegExp = "/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/i";
        preg_match($sRegExp, $words, $Match);
        if (count($Match) > 0) {
            $image = $Match['1'];
            $image = str_replace(".JPG", ".jpg", $image);
        }
        if (!empty($image)) {
            $image =
                '<img width="' . $width . '" height="' . $height . '" ' . $style . ' border="0" src="' . $image . '"/>';
        }
        return $image;
    }

    static public function isItemActive($item, $route)
    {
        if (isset($item['url']) && isset($item['label']) && is_array($item['url']) && isset($item['id']) &&
            strtolower($item['label']) === strtolower($item['id'])
        ) {
            if (count($item['url']) > 1) {
                foreach (array_splice($item['url'], 1) as $name => $value) {
                    if (!isset($_GET[$name]) || $_GET[$name] != $value) return false;
                }
            }
            return true;
        } elseif (isset($item['url']) && is_array($item['url']) && !strcasecmp(trim($item['url'][0], '/'), $route)) {
            if (count($item['url']) > 1) {
                foreach (array_splice($item['url'], 1) as $name => $value) {
                    if (!isset($_GET[$name]) || $_GET[$name] != $value) return false;
                }
            }
            return true;
        }
        return false;
    }
}
